package com.blog.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blog.user.entity.UserCollection;
import com.blog.user.mapper.UserCollectionMapper;
import com.blog.user.service.UserCollectionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zw
 * @since 2021-12-08
 */
@Service
public class UserCollectionServiceImpl extends ServiceImpl<UserCollectionMapper, UserCollection> implements UserCollectionService {
    @Autowired
    UserCollectionMapper userCollectionMapper;

    @Override
    public boolean isCollected(String userId, Integer articleId) {
        QueryWrapper<UserCollection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId).eq("article_id",articleId);
        UserCollection userCollection = this.getOne(queryWrapper);
        if(userCollection == null)
            return  false;
        else
            return true;
    }

    @Override
    public void insertCollection(String userId,Integer articleId){
        UserCollection userCollection=new UserCollection();
        userCollection.setUserId(userId);
        userCollection.setArticleId(articleId);
        userCollectionMapper.insert(userCollection);
    }
    @Override
    public void pageQuery(Page<UserCollection> pageInfo, String userId) {
        if(userId == null||userId.isEmpty())
            return ;
        QueryWrapper<UserCollection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId).orderBy(true,true,"id");
        this.page(pageInfo,queryWrapper);
    }
}
